package com.yangli.ahies.study.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * @author ly
 * @description
 * @data 2022/4/18
 */
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.valueOf(this.val));
        ListNode temp = this.next;
        while (temp != null) {
            sb.append("->").append(temp.val);
            temp = temp.next;
        }
        return sb.toString();
    }

    Map<Class<?>, Object> contanier = new HashMap();

    public <T> void put(Class<T> type, T instance) {
        //防止恶意客户端用原生类型攻击，进行检查==>CheckedCollection ==>type.cast(instance)
        Objects.requireNonNull(instance);
        if (type.isInstance(instance)) {
            throw new ClassCastException();
        }
        contanier.put(type, type.cast(instance));
    }

    public <T extends Annotation> T get(Class<T> type) {
        return type.cast(contanier.get(type));
    }

    static Annotation etAnnotation(AnnotatedElement element, String annotationName) throws ClassNotFoundException {
        Class<?> annotation = Class.forName(annotationName);
        return element.getAnnotation(annotation.asSubclass(Annotation.class));
    }

    static Stream<BigInteger> primes() {
        return Stream.iterate(BigInteger.ONE, BigInteger::nextProbablePrime);

    }

    public static void main(String[] args) {
/*        primes().map(p -> BigInteger.valueOf(2).pow(p.intValueExact()).subtract(BigInteger.ONE)).filter(x -> x.isProbablePrime(50)).limit(20).forEachOrdered().forEach(System.out::println);


        Stream.of(1, 2, 3).collect(Collectors.toMap(BigDecimal::new, x->x));*/

        Stream<String> data  =  Stream.of("1","2","3");
        data.iterator();
        Iterable<String> iterable =  iterableOf(data);
    Objects.requireNonNull(iterable);
    iterable.forEach(System.out::println);
        iterable.iterator();
    }

    static <E> Iterable<E> iterableOf(Stream<E> stream) {
        return stream::iterator;
    }



}
